package com.wang.vedio.application;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Process;
import android.util.Log;
import android.webkit.WebView;

import androidx.annotation.NonNull;
import androidx.multidex.MultiDexApplication;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.shuyu.gsyvideoplayer.cache.CacheFactory;
import com.shuyu.gsyvideoplayer.player.PlayerFactory;
import com.vondear.rxtool.RxTool;

import java.util.ArrayList;
import java.util.List;

import tv.danmaku.ijk.media.exo2.Exo2PlayerManager;
import tv.danmaku.ijk.media.exo2.ExoPlayerCacheManager;


/**
 * 描述：添加类的描述
 *
 * @author
 * @time 2018/10/15
 */
public class APP extends MultiDexApplication {
    private static APP applicationContext;
    private static List<Activity> activityList = new ArrayList<Activity>();
    public static String registrationID;

    static {//static 代码段可以防止内存泄露
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                return new ClassicsHeader(context);//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Scale);
            }
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        applicationContext = this;
        Log.i("time:APP", System.currentTimeMillis() + "");
        initThirdSDK();
    }

    private void initThirdSDK() {
        RxTool.init(this);
        PlayerFactory.setPlayManager(Exo2PlayerManager.class);
        CacheFactory.setCacheManager(ExoPlayerCacheManager.class);
        // 以下逻辑只在主进程初始化时执行
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WebView.setDataDirectorySuffix(Process.myPid() + "");
        }
    }

    public void exitApp() {
        for (Activity activity : activityList) {
            activity.finish();
        }
        /**
         * 清楚一些缓存
         */
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(1);
    }

    public void finishAllActivity() {
        for (Activity activity : activityList) {
            activity.finish();
        }
    }

    public void add(Activity activity) {
        activityList.add(activity);
    }

    public static APP getInstance() {
        return applicationContext;
    }
}
